We perform hydrodynamical simulations of the accretion of pebbles and rocks onto protoplanets of a few hundred kilometres in radius, including two-way drag force coupling between particles and the protoplanetary disc gas. Particle streams interacting with the gas far out within the Hill sphere of the protoplanet spiral into a prograde circumplanetary disc. Material is accreted onto the protoplanet due to stirring by the turbulent surroundings. We speculate that the trend for prograde rotation among the largest asteroids is primordial and that protoplanets accreted 10%-50% of their mass from pebbles and rocks during the gaseous solar nebula phase. Our model also offers a possible explanation for the narrow range of spin periods observed among the largest bodies in the asteroid and trans-Neptunian belts, and predicts that 1000 km-scale Kuiper belt objects that have not experienced giant impacts should preferentially spin in the prograde direction.